package com.css.zfzx.fxpg.modules.economicloss.middle.repository.impl;

import com.css.zfzx.fxpg.modules.economicloss.middle.repository.entity.FxEconomyLossMiddleEntity;
import com.css.zfzx.fxpg.modules.economicloss.middle.viewobjects.FxEconomyLossMiddleQueryParams;
import com.css.zfzx.fxpg.modules.economicloss.middle.repository.FxEconomyLossMiddleNativeRepository;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;
/**
 * @author andy
 * @date 2020-12-02
 */
@Repository
public class FxEconomyLossMiddleNativeRepositoryImpl implements FxEconomyLossMiddleNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(FxEconomyLossMiddleNativeRepositoryImpl.class);


    @Override
    public Page<FxEconomyLossMiddleEntity> queryFxEconomyLossMiddles(FxEconomyLossMiddleQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fx_economy_loss_middle ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEarthquakeIntensity())) {
            whereSql.append(" and earthquake_intensity = :earthquakeIntensity");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDamageDegree())) {
            whereSql.append(" and damage_degree = :damageDegree");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCalculatedValue())) {
            whereSql.append(" and calculated_value = :calculatedValue");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCorrectedValue())) {
            whereSql.append(" and corrected_value = :correctedValue");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxEconomyLossMiddleEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_economy_loss_middle ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEarthquakeIntensity())) {
            query.setParameter("earthquakeIntensity", queryParams.getEarthquakeIntensity());
            countQuery.setParameter("earthquakeIntensity", queryParams.getEarthquakeIntensity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDamageDegree())) {
            query.setParameter("damageDegree", queryParams.getDamageDegree());
            countQuery.setParameter("damageDegree", queryParams.getDamageDegree());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCalculatedValue())) {
            query.setParameter("calculatedValue", queryParams.getCalculatedValue());
            countQuery.setParameter("calculatedValue", queryParams.getCalculatedValue());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCorrectedValue())) {
            query.setParameter("correctedValue", queryParams.getCorrectedValue());
            countQuery.setParameter("correctedValue", queryParams.getCorrectedValue());
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<FxEconomyLossMiddleEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<FxEconomyLossMiddleEntity> fxEconomyLossMiddlePage = new PageImpl<>(list, pageable, count.longValue());
        return fxEconomyLossMiddlePage;
    }

    @Override
    public List<FxEconomyLossMiddleEntity> getFxEconomyLossMiddles(FxEconomyLossMiddleQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from fx_economy_loss_middle ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEarthquakeIntensity())) {
            whereSql.append(" and earthquake_intensity = :earthquakeIntensity");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDamageDegree())) {
            whereSql.append(" and damage_degree = :damageDegree");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCalculatedValue())) {
            whereSql.append(" and calculated_value = :calculatedValue");
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCorrectedValue())) {
            whereSql.append(" and corrected_value = :correctedValue");
        }
        whereSql.append(" ORDER BY earthquake_intensity+1, damage_degree");
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), FxEconomyLossMiddleEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from fx_economy_loss_middle ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if(!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getEarthquakeIntensity())) {
            query.setParameter("earthquakeIntensity", queryParams.getEarthquakeIntensity());
            countQuery.setParameter("earthquakeIntensity", queryParams.getEarthquakeIntensity());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getDamageDegree())) {
            query.setParameter("damageDegree", queryParams.getDamageDegree());
            countQuery.setParameter("damageDegree", queryParams.getDamageDegree());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCalculatedValue())) {
            query.setParameter("calculatedValue", queryParams.getCalculatedValue());
            countQuery.setParameter("calculatedValue", queryParams.getCalculatedValue());
        }
        if(!PlatformObjectUtils.isEmpty(queryParams.getCorrectedValue())) {
            query.setParameter("correctedValue", queryParams.getCorrectedValue());
            countQuery.setParameter("correctedValue", queryParams.getCorrectedValue());
        }
        List<FxEconomyLossMiddleEntity> list = query.getResultList();
        return list;
    }
}
